#include<stdio.h>
#include<string.h>                                                              
#include<unistd.h>
#include<pthread.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>

#define SERVERIP "192.168.1.15"
#define SERVERPORT 8000
#define BUFSIZE 1024

int main(int argc, char* argv[])
{
    // 创建一个套接字
    int lfd = socket(AF_INET,SOCK_STREAM,0);
    if(lfd == -1)
    {
        perror("socket error");
        exit(1);
    }
    // 绑定端口号和ip
    struct sockaddr_in seraddr;
    seraddr.sin_family = AF_INET;
    seraddr.sin_port = htons(SERVERPORT);
    inet_pton(AF_INET,SERVERIP,&seraddr.sin_addr.s_addr); // 目的ip的绑定
    // 建立连接请求
    connect(lfd,(struct sockaddr*)&seraddr,sizeof(seraddr));
    
    char buf[BUFSIZE];
    int ret;
    while(1)
    {
        int pid = fork();
        if(pid == 0)
        {
            ret = read(STDIN_FILENO,buf,BUFSIZE);
            write(lfd,buf,ret);
        }
        else
        {
            ret = read(lfd,buf,BUFSIZE);
            write(STDOUT_FILENO,buf,ret);
        }
        
    }

    return 0;
}

